#include <bits/stdc++.h>
using namespace std;
#define N 1000005
typedef struct Node
{
    int num, val;
    int left, right;
}Node;
Node node[N];
int p = 1;

int getDepth(int r)
{
    if(r == 0)
        return 0;
    return max(getDepth(node[r].left), getDepth(node[r].right)) + 1; 
}
int main()
{
    int n;
    cin>>n;
    for(int i = 1; i <= n; ++i)
        cin>>node[i].left>>node[i].right;
    cout<<getDepth(1);
    return 0;
}
